﻿using Com.Aote.Behaviors;
using Com.Aote.ObjectTools;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Com.Aote.Pages
{
    public partial class 平均气量 : UserControl
    {
        PagedList daninfosList = new PagedList();
        SearchObject userSearch = new SearchObject();
        String sql = "";
        String sumHql = "";
        int pageIndex = 0;

        public 平均气量()
        {
            // Required to initialize variables
            InitializeComponent();
            daninfos.ItemsSource = daninfosList;
            daninfosearch.DataContext = userSearch;
        }


        private void dansearchbutton_Click(object sender, RoutedEventArgs e)
        {
            String gasmeterstyle = ui_gasmeterstyle.SelectedValue.ToString();

            DateTime d1 = ui_handdate.SelectedDate.GetValueOrDefault();

            DateTime d2 = ui_handdate2.SelectedDate.GetValueOrDefault().AddMonths(1).AddDays(-1 * (ui_handdate2.SelectedDate.GetValueOrDefault().Day)); 

            int ys = d2.Year - d1.Year;
            int ms = d2.Month - d1.Month;

            int months = ys * 12 + ms;

            String gas = ui_gas.Text.ToString();

            userSearch.Search();
            if (gasmeterstyle.Equals("机表"))
            {
                sql = "select  u.f_userid,u.f_username,u.f_address,u.f_userarea,u.f_gasproperties, " +
                      "sum(h.oughtamount)/" + months + " as avergas from t_handplan h " +
                      "left join t_userfiles u " + 
                      "on h.f_userid = u.f_userid " +
                      "where h.f_state = '已抄表' and h.f_inputdate >= '" + d1 + "' and h.f_inputdate <= '" + d2 + "' and " + userSearch.Condition +  
                      " group by u.f_userid,u.f_username,u.f_address,u.f_gasproperties,u.f_userarea " + 
                      "having sum(h.oughtamount)/" + months + "<" + gas  +
                      " order by sum(h.oughtamount)/" + months;

                sumHql = "select  u.f_userid,u.f_username,u.f_address,u.f_userarea,u.f_gasproperties, " +
                      "sum(h.oughtamount)/" + months + " as avergas from t_handplan h " +
                      "left join t_userfiles u " +
                      "on h.f_userid = u.f_userid " +
                      "where h.f_state = '已抄表' and  h.f_inputdate >= '" + d1 + "' and h.f_inputdate <= '" + d2 + "' and " + userSearch.Condition +
                      " group by u.f_userid,u.f_username,u.f_address,u.f_gasproperties,u.f_userarea " +
                      "having sum(h.oughtamount)/" + months + "<" + gas;

            }
            else if (gasmeterstyle.Equals("卡表"))
            {
                sql = "select u.f_userid,u.f_username,u.f_address,u.f_userarea,u.f_gasproperties, " +
                      "sum(h.f_pregas)/" + months + " as avergas "+
                      " from t_sellinggas h " + 
                      " left join t_userfiles u " + 
                      " on h.f_userid = u.f_userid " +
                      "where h.f_gasmeterstyle = '卡表' and  h.f_deliverydate >= '"+ d1 +"' and h.f_deliverydate  <= '" +　d2 + "' and h.f_payfeevalid = '有效' and " + userSearch.Condition + 
                      "group by u.f_userid,u.f_username,u.f_address,u.f_gasproperties,u.f_userarea  " +
                      "having sum(h.f_pregas)/" + months + "<" + gas + 
                      "order by sum(h.f_pregas)/" + months ;


                sumHql = "select u.f_userid,u.f_username,u.f_address,u.f_userarea,u.f_gasproperties, " +
                      "sum(h.f_pregas)/" + months + " as avergas "+
                      " from t_sellinggas h " + 
                      " left join t_userfiles u " + 
                      " on h.f_userid = u.f_userid " +
                      "where h.f_gasmeterstyle = '卡表' and h.f_deliverydate >= '" + d1 + "' and h.f_deliverydate  <= '" + d2 + "' and h.f_payfeevalid = '有效' and " + userSearch.Condition +
                      "group by u.f_userid,u.f_username,u.f_address,u.f_gasproperties,u.f_userarea  " +
                      "having sum(h.f_pregas)/" +months + "<" + gas ;
            }
            else
            {
                MessageBox.Show("请选择正确的表类型（机表-卡表~）");
                return;
            }

            daninfosList.WebClientInfo = Application.Current.Resources["dbclient"] as WebClientInfo;
            daninfosList.LoadOnPathChanged = false;
            daninfosList.Names = "f_userid,f_username,f_address,f_userarea,f_gasproperties,avergas";
            daninfosList.Path = "sql";
            daninfosList.SumHQL = sumHql;
            daninfosList.SumNames = ",";
            daninfosList.PageSize = pager.PageSize;
            daninfosList.HQL = sql;
            daninfosList.PageIndex = pageIndex;
            daninfosList.Load();
        }

        private void ui_pager_PageIndexChanged(object sender, EventArgs e)
        {
            daninfosList.PageIndex = pager.PageIndex;
        }
    }
}